package com.qf.smsplatfrom.service.impl;

import com.qf.smsplatfrom.constans.ResultCode;
import com.qf.smsplatfrom.events.UpdateDescribeEvens;
import com.qf.smsplatfrom.exceptions.UpdateDataException;
import com.qf.smsplatfrom.mapper.DescribeMapper;
import com.qf.smsplatfrom.pojo.CheckType;
import com.qf.smsplatfrom.pojo.TDescribe;
import com.qf.smsplatfrom.service.DescribeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DescribeServiceImpl implements DescribeService {
    private DescribeMapper describeMapper;

    private ApplicationContext context;

    @Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    @Autowired
    public void setDescribeMapper(DescribeMapper describeMapper) {
        this.describeMapper = describeMapper;
    }

    @Override
    public void addDescribe(TDescribe tDescribe) {
        if(tDescribe.isNull(CheckType.ADD)){
            throw new UpdateDataException("没有传递必须传递的数据", ResultCode.DATA_ISNULL);
        }
        describeMapper.insertDescribe(tDescribe);
        context.publishEvent(new UpdateDescribeEvens());
    }

    @Override
    public void modifyDescribe(TDescribe tDescribe) {

        if(tDescribe.isNull(CheckType.UPDATE)){
            throw new UpdateDataException("没有传递必须传递的数据", ResultCode.DATA_ISNULL);
        }
        describeMapper.updateDescribe(tDescribe);
        context.publishEvent(new UpdateDescribeEvens());
    }

    @Override
    public void removeDescribe(List<Long> ids) {
        describeMapper.deleteDescribe(ids);
        context.publishEvent(new UpdateDescribeEvens());
    }
}
